import { defineComponent, reactive, ref } from "vue";
import Page from "@/components/public/page/page";
import { CardWithTitle } from "@/components/public/card/card";
import LTable from "@/components/public/lTable/lTable";
import promotionChannelManageService from "@/modules/individualOrdersManagement/promotion/promotionChannelManage.service";
import { EditOutlined, SearchOutlined } from "@ant-design/icons-vue";
import ChannelAdd from "./ChannelAdd/ChannelAdd";
import ChannelSaler from "./ChannelSaler/ChannelSaler";
export default defineComponent({
  name: "promotionChannelManage",
  setup() {
    const channelAddRef = ref(null as any);
    const ChannelSalerRef = ref(null as any);
    const state = reactive({
      toolbar: {
        left: ["refresh", "add"],
        right: ["filter", "search"],
        add: {
          handler: () => {
            channelAddRef.value.open();
          }
        },
      },
      dataSource: [],
      columns: [
        {
          title: "ID",
          dataIndex: "id",
          key: "id",
          align: "center",
          width: "80px",
        },
        {
          title: "渠道名称",
          dataIndex: "name",
          align: "center",
          width: "200px",
        },
        {
          title: "联系人",
          dataIndex: "username",
          align: "center",
          width: "200px",
        },
        {
          title: "联系电话",
          dataIndex: "phone",
          align: "center",
          width: "200px",
        },
        {
          title: "创建时间",
          dataIndex: "create_time",
          align: "center",
          width: "200px",
        },
        {
          fixed: "right",
          title: "操作",
          dataIndex: "operation",
          align: "center",
          width: "240px",
        },
      ],
      loading: false,
      searchForm: {
        name: {
          type: "input",
          label: "渠道名称",
          attrs: { placeholder: "请输入渠道名称..." },
        },
        username: {
          type: "input",
          label: "联系人姓名",
          attrs: { placeholder: "请输入联系人姓名..." },
        },
      },
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
      },
    } as any);
    // 表格状态变更
    const tableChangeHandler = (pag: { pageSize: number; current: number }) => {
      state.pagination.current = pag.current;
      state.pagination.pageSize = pag.pageSize;
    };
    const $service = new promotionChannelManageService();
    const LTableRef = ref(null as any);
    const editSuccess = () => {
      LTableRef.value.$fetchTableData();
    }
    return () => (
      <Page>
        <CardWithTitle title="渠道列表">
          <LTable
            ref={LTableRef}
            service={$service}
            toolbar={state.toolbar}
            v-model={state}
          >
            <a-table
              bordered={true}
              scroll={{ x: "max-content" }}
              dataSource={state.dataSource}
              columns={state.columns}
              pagination={state.pagination}
              onChange={tableChangeHandler}
              loading={state.loading}
              rowKey="id"
              v-slots={{
                bodyCell: ({ column, record }: any) => {
                  // 操作栏
                  if (column.dataIndex === "operation") {
                    return (
                      <div class="operation">
                        <a-button
                          type="primary"
                          size="small"
                          v-slots={{
                            icon: () => <EditOutlined />,
                          }}
                          onClick={() => { channelAddRef.value.open(record) }}
                        >
                          编辑
                        </a-button>
                        <a-button
                          type="primary"
                          size="small"
                          v-slots={{
                            icon: () => <SearchOutlined />,
                          }}
                          onClick={() => { ChannelSalerRef.value.open(record.id) }}
                        >
                          查看渠道人员
                        </a-button>
                      </div>
                    );
                  }
                },
              }}
            />
          </LTable>
        </CardWithTitle>
        <ChannelAdd ref={channelAddRef} onEditSuccess={editSuccess} />
        <ChannelSaler ref={ChannelSalerRef} />
      </Page>
    );
  },
});
